package com.zhiyou.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

@Component
public class MyUtil {

    public String sendSms(String tel){
        /**
         * 1.导包
         * 2.把实例代码导到项目里
         * 3.改accessKeyId  accessSecret
         * 4.测试
         * DefaultProfile.getProfile("default", "<accessKeyId>", "<accessSecret>");
         */
        String s = ranString();
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tRemYgBWpqiVufqNPSp", "5qJaxUmoCXc20lWpydRY4QyDAh4JVP");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "阿里云短信测试");
        request.putQueryParameter("TemplateCode", "SMS_154950909");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+s+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return s;
    }

    public String ranString(){
        int a = (int) (Math.random()*10);
        int b = (int) (Math.random()*10);
        int c = (int) (Math.random()*10);
        int d = (int) (Math.random()*10);
        return ""+a+b+c+d;
    }
}
